package site.qjdb.service.impl;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import site.qjdb.dao.CommentDao;
import site.qjdb.dao.ReplyDao;
import site.qjdb.dao.WantDao;
import site.qjdb.pojo.Comment;
import site.qjdb.pojo.Reply;
import site.qjdb.pojo.User;
import site.qjdb.pojo.Want;
import site.qjdb.service.ReplyService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReplyServiceImpl implements ReplyService {
	@Resource
	private ReplyDao replyDao;
	@Resource
	private CommentDao commentDao;
	@Resource
	private WantDao wantDao;

	@Override
	public void addReply(Reply reply) {
		replyDao.addReply(reply);
	}

	@Override
	public void deleteReplyByComment(Integer rid, Integer uid, Integer superiorId) throws Exception {
		Comment comment = new Comment();
		comment.setCommentId(superiorId);
		comment.setIsDel(0);
		Map<String, Object> map = new HashMap<>();
		map.put("comment", comment);
		List<Comment> commentList = commentDao.selectCommentAll(map);
		if (commentList != null && commentList.size() > 0) {
			// 查询回复
			Reply reply = new Reply();
			// 回复id
			reply.setRid(rid);
			// 上级id
			reply.setSuperiorId(superiorId);
			// 类型：留言
			reply.setType(0);
			// 只能删除自己的留言
			User user = new User();
			user.setUid(uid);
			reply.setUser(user);
			// 未删除
			reply.setIsDel(0);
			Reply newReply = findReply(reply);
			if (newReply != null) {
				Reply _reply = new Reply();
				_reply.setRid(rid);
				_reply.setIsDel(1);
				replyDao.updateReply(_reply);
				return;
			}
		}
		throw new Exception();
	}

	@Override
	public Reply findReply(Reply reply) {
		Map<String, Object> map = new HashMap<>();
		map.put("reply", reply);
		List<Reply> replyList = replyDao.selectReplyAll(map);
		if (replyList == null || replyList.size() == 0) {
			return null;
		} else {
			return replyList.get(0);
		}
	}

	@Override
	public void deleteReplyByWant(Integer rid, Integer uid, Integer superiorId) throws Exception {
		Want want = new Want();
		want.setWid(superiorId);
		want.setIsDel(0);
		Map<String, Object> map = new HashMap<>();
		map.put("want", want);
		List<Want> wantList = wantDao.selectWantAll(map);
		if (wantList != null && wantList.size() > 0) {
			// 查询回复
			Reply reply = new Reply();
			// 回复id
			reply.setRid(rid);
			// 上级id
			reply.setSuperiorId(superiorId);
			// 类型：求购信息
			reply.setType(1);
			// 只能删除自己的留言
			User user = new User();
			user.setUid(uid);
			reply.setUser(user);
			// 未删除
			reply.setIsDel(0);
			Reply newReply = findReply(reply);
			if (newReply != null) {
				Reply _reply = new Reply();
				_reply.setRid(rid);
				_reply.setIsDel(1);
				replyDao.updateReply(_reply);
				return;
			}
		}
		throw new Exception();
	}

	@Override
	public List<Reply> findReplyAll(Integer superiorId) {
		Map<String, Object> map = new HashMap<>();
		Reply reply = new Reply();
		reply.setIsDel(0);
		reply.setSuperiorId(superiorId);
		map.put("reply", reply);
		map.put("orderBy", "reply.time desc");
		List<Reply> replyList = replyDao.selectReplyAll(map);
		if (replyList == null || replyList.size() == 0) {
			return null;
		} else {
			return replyList;
		}
	}

}
